package com.customization.huangwm.cronJob.executeActions.SW007;

import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DateTools;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * @Author Huangwm
 * @Date 2023-09-25
 * @Version 1.0
 * @Description TODO
 **/
public class UF_007_Plan extends BaseCronJob {
    private LogTool log = new LogTool("/log/SW007/UF_007_Plan", false);
    private static DateTools dateTools = new DateTools();

    @Override
    public void execute() {
        log.writeLog("超滤膜换膜计划开始");
        RecordSet rs = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        try{
            rs.execute("select * from uf_007_Plan where Nvl(status,0)=0 and activedate='"+dateTools.getCurrentDate()+"'");
            log.writeLog("本次更新的记录数 "+rs.getCounts());
            while (rs.next()){
                String billid=  rs.getString("id");
                String code_old = rs.getString("code1");//旧膜
                String loc_old = rs.getString("loc1");
                String code_new= rs.getString("code2");//新膜
                String loc_new = rs.getString("loc2");

                //查询旧膜层号信息
                rs2.execute("select tank,cassette,layer,locnum from uf_men_detail where id="+code_old);
                rs2.next();
                String tank_old = rs2.getString("tank");
                String cassette_old = rs2.getString("cassette");
                String layer_old = rs2.getString("layer");
                String locnum_old = rs2.getString("locnum");

                log.writeLog("旧膜层号信息 tank="+tank_old+" cassette="+cassette_old+" layer="+layer_old+" locnum="+locnum_old);
                Object[] params_new = {
                        loc_old,tank_old,cassette_old,layer_old,locnum_old,code_new
                };
                log.writeLog("更新新膜主表信息 "+ JSONObject.toJSONString(params_new));


                //查询新膜层号信息
                rs3.execute("select tank,cassette,layer,locnum from uf_men_detail where id="+code_new);
                rs3.next();
                String tank_new = rs3.getString("tank");
                String cassette_new = rs3.getString("cassette");
                String layer_new = rs3.getString("layer");
                String locnum_new = rs3.getString("locnum");
                log.writeLog("新膜层号信息 tank="+tank_new+" cassette="+cassette_new+" layer="+layer_new+" locnum="+locnum_new);
                Object[] params_old = {
                        loc_new,tank_new,cassette_new,layer_new,locnum_new,code_old
                };
                log.writeLog("更新旧膜主表信息 "+JSONObject.toJSONString(params_old));


                //更新原本的膜路径明细为失效
                rst.execute("update  uf_men_detail_dt3 set loc_status=1 where mainid in("+code_old+","+code_new+")");

                //loc1对应code2 新模
                boolean r1 = rst.executeUpdate("update uf_men_detail  set loc_full=?,tank=?,cassette=?,layer=?,locnum=? where id=?",params_new);
                log.writeLog("更新新膜主表结果 "+r1);
                //插入新膜路径明细
                Object[] objects_new = {
                        code_new,0,dateTools.getCurrentDate(),loc_old
                };
                log.writeLog("插入新膜路径信息 "+ JSONObject.toJSONString(objects_new));
                boolean r2 = rst.executeUpdate("insert into uf_men_detail_dt3(mainid,loc_status,dateStr,loc)values(?,?,?,?)",objects_new);
                log.writeLog("插入新膜路径信息 "+ r2);

                //loc2对应code1 更新旧膜信息
                boolean r3 = rst.executeUpdate("update uf_men_detail  set loc_full=?,tank=?,cassette=?,layer=?,locnum=? where id=?",params_old);
                log.writeLog("更新旧膜主表结果 "+r3);
                //插入旧膜路径明细
                Object[] objects_old = {
                        code_old,0,dateTools.getCurrentDate(),loc_new
                };
                log.writeLog("插入旧膜路径信息 "+ JSONObject.toJSONString(objects_old));
                boolean r4 = rst.executeUpdate("insert into uf_men_detail_dt3(mainid,loc_status,dateStr,loc)values(?,?,?,?)",objects_old);
                log.writeLog("插入旧膜路径结果 "+ r4);

                rst.execute("update uf_007_Plan set status=1 where id="+billid);
            }

            rst.commit();

            log.writeLog("超滤膜换膜计划结束");
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            log.writeLog("换膜计划更新异常 "+e.getMessage());
        }

    }
}
